<template>
  <div class="prod-prodList">
    <div class="prod-prodList-nr">
      <div class="prod-prodList-input" v-if="flag">
        <el-row>
          <el-form ref="form" :model="ruleForm" label-width="80px">
            <el-col :span="6">
              <el-form-item label="产品名字">
                <el-input
                  v-model="ruleForm.prodName"
                  placeholder="请输入产品名字"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select v-model="ruleForm.status" placeholder="请选择状态">
                  <el-option label="未上架" :value="0"></el-option>
                  <el-option label="上架" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-button type="primary" icon="el-icon-search" @click="getList"
                >搜索</el-button
              >
              <el-button icon="el-icon-delete" @click="clearAll"
                >清空</el-button
              >
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="prod-prodList-btn">
        <div>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="addData"
            >新增</el-button
          >
          <el-button
            type="danger"
            size="small"
            :disabled="!multipleSelection.length"
            @click="delAll"
            >批量删除</el-button
          >
        </div>
        <div>
          <el-button icon="el-icon-refresh" circle @click="refresh"></el-button>
          <el-button icon="el-icon-search" circle @click="getif"></el-button>
        </div>
      </div>
      <div class="prod-prodList-tag">
        <el-tag
          >当前表格已选择{{ multipleSelection.length }}项
          <span>清空</span>
        </el-tag>
      </div>
      <div class="prod-prodList-table">
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          highlight-current-row
          stripe
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"> </el-table-column>

          <el-table-column prop="prodName" label="产品名字" width="224">
          </el-table-column>
          <el-table-column
            prop="oriPrice"
            label="商品原价"
            width="223"
          ></el-table-column>
          <el-table-column prop="price" label="商品现价" width="223">
          </el-table-column>
          <el-table-column prop="totalStocks" label="商品库存" width="223">
          </el-table-column>
          <el-table-column prop="name" label="产品图片" width="150">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                fit="cover"
                :src="scope.row.pic"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="150">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 1">上架</el-tag>
              <el-tag v-else>未上架</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="350">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.row)"
                >修改</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="prod-prodList-page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      flag: true,
      tableData: [],
      ruleForm: {
        prodName: "",
        status: "",
      },
      multipleSelection: [],
      current: 1,
      size: 10,
      pages: 1,
      total: 13,
    };
  },
  mounted() {
    // localStorage.setItem(
    //   "token",
    //   "TezgiF8CXQaif3FAGlhzL5j7OsbnwvNpQAMxn801IaL1XawEeI9nGqOLrznroqYJ"
    // );
    this.getList();
  },
  methods: {
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete("/api/prod/prod", {
              data: [row.prodId],
              headers: {
                Authorization: localStorage.getItem("token"),
              },
            })
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 清空
    clearAll() {
      this.ruleForm = {
        prodName: "",
        status: "",
      };
    },
    // 刷新
    refresh() {
      this.ruleForm = {
        prodName: "",
        status: "",
      };
      this.getList();
    },
    // 显示隐藏
    getif() {
      this.flag = !this.flag;
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val;
      this.getList();
    },
    // 渲染
    getList() {
      console.log("getList");
      // this.$axios({
      //   url: "/admin/user/page",
      //   method: "get",
      //   params: {
      //     current: 1,
      //     size: 10,
      //     t: 1742002425161,
      //     Authorization:"QtdS5tlp0c17aZ1FVwWktycJEl3OSRwUlODT8CU8BbbmplUzKM/BAY+h1T90XP1y"
      //   }
      // })
      this.$axios
        .get("/api/prod/prod/page", {
          params: {
            ...this.ruleForm,
            current: this.current,
            size: this.size,
            t: new Date().getTime(),
          },
          headers: {
            Authorization: localStorage.getItem("token"),
          },
        })
        .then((res) => {
          // console.log(res);
          if (res.data.code == "00000") {
            this.tableData = res.data.data.records;
            console.log(this.tableData);
            // this.current = res.data.data.current;
            this.size = res.data.data.size;
            this.total = res.data.data.total;
            this.pages = res.data.data.pages;
            console.log(this.current, this.size, this.total, this.pages);
          }
        });
    },
    // 新增
    addData() {
      this.$router.push("/prod/prodInfo");
    },
    // 修改
    edit(row) {
      this.$router.push({
        path: "/prod/prodInfo",
        query: { id: row.id },
      });
    },
    // 批量删除
    delAll() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete("/api/prod/prod", {
              data: this.multipleSelection.map((item) => item.prodId),
              headers: {
                Authorization: localStorage.getItem("token"),
              },
            })
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.prod-prodList-tag {
  margin-bottom: 20px;
}
.prod-prodList-page {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}
.prod-prodList {
  padding: 20px;
  background-color: #eef1f2;
}
.prod-prodList-nr {
  background-color: #fff;
  padding: 20px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.prod-prodList-btn {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
</style>